﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;


namespace DEMI_webapp
{
    public partial class WydaniaMagazynowe : System.Web.UI.Page
    {
        System.Configuration.ConnectionStringSettings connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["conString"];
        SqlConnection con;
        SqlDataReader reader;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Calendar1.SelectedDate = DateTime.Today;

                con = new SqlConnection(connString.ConnectionString);
                con.Open();

                SqlCommand magazynierzy = new SqlCommand("select * from Magazynierzy", con);
                reader = magazynierzy.ExecuteReader();
                while (reader.Read())
                {
                    string p = reader.GetString(reader.GetOrdinal("imie"));
                    p += " " + reader.GetString(reader.GetOrdinal("nazwisko"));
                    List_Magazynier.Items.Add(p);
                }
                reader.Close();

                SqlCommand klienci = new SqlCommand("select * from Klienci", con);
                reader = klienci.ExecuteReader();
                while (reader.Read())
                {
                    string p="";
                    try
                    {
                        p += reader.GetString(reader.GetOrdinal("nazwa"));
                        p += " ";
                    }
                    catch (Exception)
                    {
                    
                    }
                    p += reader.GetString(reader.GetOrdinal("imie"));
                    p += " " + reader.GetString(reader.GetOrdinal("nazwisko"));
                    List_Klient.Items.Add(p);
                }
                reader.Close();

                SqlCommand kapelusze = new SqlCommand("select distinct nazwa from kapelusze", con);
                reader = kapelusze.ExecuteReader();
                while (reader.Read())
                {
                    string p = reader.GetString(reader.GetOrdinal("nazwa"));
                    List_Nazwa.Items.Add(p);
                }
                reader.Close();

                SqlCommand kolory = new SqlCommand("select kolor from Kapelusze where nazwa= @Nazwa", con);
                SqlParameter param = new SqlParameter();
                param.ParameterName = "@Nazwa";
                param.Value = List_Nazwa.SelectedItem.Text;
                kolory.Parameters.Add(param);
                reader = kolory.ExecuteReader();
                while (reader.Read())
                {
                    string p = reader.GetString(reader.GetOrdinal("kolor"));
                    List_Kolor.Items.Add(p);
                }
                reader.Close();
                con.Close();
            } 
        }

		protected void Button_Wydanie_Click (object sender, ImageClickEventArgs e)
        {
            con = new SqlConnection(connString.ConnectionString);
            con.Open();

            SqlCommand select_ilosc = new SqlCommand("select ilosc from Kapelusze where nazwa= @Nazwa and kolor=@Kolor", con);
            SqlParameter param9 = new SqlParameter();
            param9.ParameterName = "@Nazwa";
            param9.SqlDbType = SqlDbType.VarChar;
            param9.Value = List_Nazwa.SelectedItem.Text;
            select_ilosc.Parameters.Add(param9);
            SqlParameter param10 = new SqlParameter();
            param10.ParameterName = "@Kolor";
            param10.SqlDbType = SqlDbType.VarChar;
            param10.Value = List_Kolor.SelectedItem.Text;
            select_ilosc.Parameters.Add(param10);
            int ilosc = (Int32)select_ilosc.ExecuteScalar();

            try { Convert.ToInt32(TB_Ilość.Text); }
            catch { L_Uwaga.Text = "Podana ilość kapelusza jest błedna. Nie zatwierdzono"; return; }

            if (Convert.ToInt32(TB_Ilość.Text) > ilosc || TB_Ilość.Text == "")
            {
                L_Uwaga.Text = "Podana ilość kapelusza jest większa od ilości w magazynie. Nie zatwierdzono";
            }
            else
            {
            SqlCommand magazynier = new SqlCommand("select id from Magazynierzy where imie= @Imie and nazwisko=@Nazwisko", con);
            string[] dane = List_Magazynier.SelectedItem.Text.Split(' ');
            SqlParameter param = new SqlParameter();
            param.ParameterName = "@Imie";
            param.Value = dane[0];
            magazynier.Parameters.Add(param);
            SqlParameter param1 = new SqlParameter();
            param1.ParameterName = "@Nazwisko";
            param1.Value = dane[1];
            magazynier.Parameters.Add(param1);
            int id_magazyniera = (Int32)magazynier.ExecuteScalar();

            int id_klienta;
            string[] dane_k = List_Klient.SelectedItem.Text.Split(' ');
            if (dane_k.Length == 3)
            {
                SqlCommand klient = new SqlCommand("select id from Klienci where nazwa=@NazwaK and imie= @ImieK and nazwisko=@NazwiskoK", con);
                SqlParameter param12 = new SqlParameter();
                param12.ParameterName = "@ImieK";
                param12.Value = dane_k[1];
                klient.Parameters.Add(param12);
                SqlParameter param13 = new SqlParameter();
                param13.ParameterName = "@NazwiskoK";
                param13.Value = dane_k[2];
                klient.Parameters.Add(param13);
                SqlParameter param14 = new SqlParameter();
                param14.ParameterName = "@NazwaK";
                param14.Value = dane_k[0];
                klient.Parameters.Add(param14);
                id_klienta = (Int32)klient.ExecuteScalar();
            }
            else
            {
                SqlCommand klient = new SqlCommand("select id from Klienci where imie= @ImieK and nazwisko=@NazwiskoK", con);
                SqlParameter param12 = new SqlParameter();
                param12.ParameterName = "@ImieK";
                param12.Value = dane_k[0];
                klient.Parameters.Add(param12);
                SqlParameter param13 = new SqlParameter();
                param13.ParameterName = "@NazwiskoK";
                param13.Value = dane_k[1];
                klient.Parameters.Add(param13);
                id_klienta = (Int32)klient.ExecuteScalar();
            }



            SqlCommand kapelusz = new SqlCommand("select id from Kapelusze where nazwa= @Nazwa and kolor=@Kolor", con);
            SqlParameter param2 = new SqlParameter();
            param2.ParameterName = "@Nazwa";
            param2.SqlDbType = SqlDbType.VarChar;
            param2.Value = List_Nazwa.SelectedItem.Text;
            kapelusz.Parameters.Add(param2);
            SqlParameter param3 = new SqlParameter();
            param3.ParameterName = "@Kolor";
            param3.SqlDbType = SqlDbType.VarChar;
            param3.Value = List_Kolor.SelectedItem.Text;
            kapelusz.Parameters.Add(param3);
            int id_kapelusza = (Int32)kapelusz.ExecuteScalar();

            SqlCommand insert = new SqlCommand("INSERT INTO Historia VALUES (0,@Data,@Id_mag,@Ilosc,@Id_kap,@Id_klienta)", con);
            SqlParameter param4 = new SqlParameter();
            param4.ParameterName = "@Data";
            param4.SqlDbType = SqlDbType.SmallDateTime;
            param4.Value = Calendar1.SelectedDate;
            insert.Parameters.Add(param4);

            SqlParameter param5 = new SqlParameter();
            param5.ParameterName = "@Id_mag";
            param5.SqlDbType = SqlDbType.Int;
            param5.Value = id_magazyniera;
            insert.Parameters.Add(param5);

            SqlParameter param6 = new SqlParameter();
            param6.ParameterName = "@Ilosc";
            param6.SqlDbType = SqlDbType.Int;
            param6.Value = TB_Ilość.Text;
            insert.Parameters.Add(param6);

            SqlParameter param7 = new SqlParameter();
            param7.ParameterName = "@Id_kap";
            param7.SqlDbType = SqlDbType.Int;
            param7.Value = id_kapelusza;
            insert.Parameters.Add(param7);

            SqlParameter param11 = new SqlParameter();
            param11.ParameterName = "@Id_klienta";
            param11.SqlDbType = SqlDbType.Int;
            param11.Value = id_klienta;
            insert.Parameters.Add(param11);

            insert.ExecuteNonQuery();


            int nowa_ilosc = ilosc - Convert.ToInt32(TB_Ilość.Text);

            SqlCommand update_ilosc = new SqlCommand("UPDATE Kapelusze SET [ilosc] = @Ilosc WHERE id = @Id_kap", con);
            SqlParameter param8 = new SqlParameter();
            param8.ParameterName = "@Ilosc";
            param8.SqlDbType = SqlDbType.Int;
            param8.Value = nowa_ilosc;
            update_ilosc.Parameters.Add(param8);
            SqlParameter param15 = new SqlParameter();
            param15.ParameterName = "@Id_kap";
            param15.SqlDbType = SqlDbType.Int;
            param15.Value = id_kapelusza;
            update_ilosc.Parameters.Add(param15);

            update_ilosc.ExecuteNonQuery();
            L_Uwaga.Text = "";
            Response.Write("<script>alert('Wydano z magazynu')</script>");
            }

            con.Close();
        
        }

        protected void List_Nazwa_SelectedIndexChanged(object sender, EventArgs e)
        {
            con = new SqlConnection(connString.ConnectionString);
            con.Open();
            List_Kolor.Items.Clear();
            SqlCommand kolory = new SqlCommand("select kolor from Kapelusze where nazwa= @Nazwa", con);
            SqlParameter param = new SqlParameter();
            param.ParameterName = "@Nazwa";
            param.Value = List_Nazwa.SelectedItem.Text;
            kolory.Parameters.Add(param);
            reader = kolory.ExecuteReader();
            while (reader.Read())
            {
                string p = reader.GetString(reader.GetOrdinal("kolor"));
                List_Kolor.Items.Add(p);
            }

            con.Close();
        }
    }
}